package com.moon;

import cn.yueliangbaba.R;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.RequestParams;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.PreferencesUtils;
import com.util.ToastUtil;
import com.util.Utils;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ActiveActivity extends BaseActivity implements OnClickListener{
	int seconds = 120;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_active);
		
		findViewById(R.id.left).setOnClickListener(this);

		if(getIntent().getExtras().getInt("flag") == 0) ((TextView) findViewById(R.id.center)).setText("账号激活");
		else ((TextView) findViewById(R.id.center)).setText("找回密码");
		
		findViewById(R.id.send).setOnClickListener(this);
		findViewById(R.id.next).setOnClickListener(this);
	}
	
	private void getCode() {
		final String phone = ((EditText)findViewById(R.id.phone)).getText().toString();
		if(!Utils.isMobileNO(phone))
		{
			ToastUtil.showToastWithAlertPic("手机号码不正确");
			return;
		}
		
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@SuppressLint("HandlerLeak")
			@Override
			public void onSuccess(String response) {
				JSONObject ruser = JSON.parseObject(response);
				if (ruser.getString("statusCode").equals("0"))
				{
					PreferencesUtils.putString(ActiveActivity.this, "sms_code", ruser.getString("code"));
					final Handler handle = new Handler()
					{
						public void handleMessage(Message msg)
						{
							if(seconds == 0)
							{
								((Button)findViewById(R.id.send)).setText("获取动态码短信");
								findViewById(R.id.send).setOnClickListener(ActiveActivity.this);
								((Button)findViewById(R.id.send)).setSelected(false);
							}
							else
							{
								((Button)findViewById(R.id.send)).setSelected(true);
								((Button)findViewById(R.id.send)).setText(String.valueOf(seconds) + "s");
								findViewById(R.id.send).setOnClickListener(null);
							}
							
							super.handleMessage(msg);
						}
					};
					
					seconds = 120;
					new Thread()
					{
						@Override
						public void run() {
							while(seconds > 0)
							{
								seconds--;
								handle.sendEmptyMessage(0);
								
								try {
									Thread.sleep(1000);
								} catch (InterruptedException e) {
									e.printStackTrace();
								}
							}
							
							handle.sendEmptyMessage(0);
						}
					}.start();
				}
				else 
				{
					ToastUtil.showToastWithAlertPic(ruser.getString("message"));
				}
			}
			
			@Override
			public void onFailure(Throwable ble) {
				super.onFailure(ble);
				ToastUtil.showToastWithAlertPic("网络状况不佳，请稍后重试...");
			}
		};
		
		RequestParams params = new RequestParams();
		params.put("phonenumber",phone);
		params.put("flag",getIntent().getExtras().getInt("flag"));
		MgqRestClient.get("get_sms_code", params, loginHandler);
	}
	
	private void vetifyCode() {
		final String phone = ((EditText)findViewById(R.id.phone)).getText().toString();
		final String code = ((EditText)findViewById(R.id.code)).getText().toString();
		
		if(!Utils.isMobileNO(phone))
		{
			ToastUtil.showToastWithAlertPic("手机号码不正确");
			return;
		}

		String smsCode = PreferencesUtils.getString(this, "sms_code");
		if(code.length() == 0 || !code.equals(smsCode))
		{
			ToastUtil.showToastWithAlertPic("验证码不正确");
			return;
		}
		
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				JSONObject ruser = JSON.parseObject(response);
				if (ruser.getString("statusCode").equals("0")) {
					if(getIntent().getExtras().getInt("flag") == 0)
					{
						Intent intent = new Intent(ActiveActivity.this,ChooseClassActivity.class);
						intent.putExtra("info", response);
						startActivity(intent);
					}
					else
					{
						Intent intent = new Intent(ActiveActivity.this,ResetPwdActivity.class);
						intent.putExtra("telephone", ((EditText)findViewById(R.id.phone)).getText().toString());
						startActivity(intent);
						finish();
					}
					
					finish();
				}
				else 
				{
					ToastUtil.showToastWithAlertPic(ruser.getString("message"));
				}
			}
			
			@Override
			public void onFailure(Throwable ble) {
				super.onFailure(ble);
				ToastUtil.showToastWithAlertPic("网络状况不佳，请稍后重试...");
			}
		};

		RequestParams params = new RequestParams();
		params.put("phonenumber",phone);
		params.put("code",code);

		MgqRestClient.get("member_active_preview", params, loginHandler);
	}

	@Override
	public void onClick(View arg0) {
		if(arg0.getId() == R.id.send)
		{
			getCode();
		}
		else if(R.id.left == arg0.getId())
		{
			finish();
		}
		else
		{
			vetifyCode();
		}
	}
}
